<?php
/**
 * Created by PhpStorm.
 * User: hulinhua
 * Date: 2018/1/25
 * Time: 上午11:29
 */

namespace app\index\model;

use think\Db;
use think\Exception;
use think\Model;
use think\Request;
use think\Validate;


class LoginModel extends Model
{

    /**
     * @var string 用户名/账号
     */
    protected $user_name;

    /**
     * @var string 手机号
     */
    protected $user_phone;

    /**
     * @var string 密码
     */
    protected $user_password;

    /**
     * @var string 重复密码
     */
    protected $user_password_rep;

    /**
     * @var string 昵称
     */
    protected $user_nickname;

    /**
     * @var string 头像
     */
    protected $user_avatar;

    /**
     * @var string 头像路径
     */
    protected $user_avatar_path;

    /**
     * @var string 性别
     */
    protected $user_sex;

    /**
     * @var string 年龄
     */
    protected $user_age;

    /**
     * @var bool 登陆是否成功
     */
    public $login_success = false;

    /**
     * @var string 登陆失败信息
     */
    public $login_error;

    /**
     * @var bool 注册是否成功
     */
    public $register_success = false;

    /**
     * @var string 注册失败信息
     */
    public $register_error;

    public function bindRequest(Request $request)
    {
        $this->user_name = $request->request('username');
        $this->user_phone = $request->request('userPhone');
        $this->user_password = $request->request('password');
        $this->user_password_rep = $request->request('password1');
        $this->user_nickname = $request->request('nickname');
        $this->user_sex = $request->request('sex');
        $this->user_age = $request->request('age');
        $this->user_avatar = $request->file('avatar');


    }



    public function checkUser($account, $pw)
    {
        $result = [
            'code'=>false,
            'msg'=>'登陆失败',
        ];
        $userData = Db::name('user')->where('user_name',trim($account))->select();


        if (!$userData){
            $result['msg']='用户名不存在，请确认后重试!';
        }
        else if ($userData[0]['user_password'] == trim($pw)){

            session('userInfo',$userData[0]);
            $result['code']=true;
            $result['msg']='登陆成功';

        }else{
            $result['msg']='用户名和密码不匹配，请确认后重试';
        }

        return $result;

    }

    public function checkRegister()
    {

        $this->register_success = true;


//         验证输入成功
        if($this->checkRegisterDate()){

            // 保存头像成功
            if($this->savaAvatar()){

                // 插入数据库
                $this->registerUser();
            }
        }


        return $this;
    }

    private function registerUser(){

        $data = [
            'user_name' => $this->user_name,
            'user_phone' => $this->user_phone,
            'user_password' => $this->user_password,
            'user_nickname' => $this->user_nickname,
            'user_avatar' => $this->user_avatar_path,
            'user_sex' => $this->user_sex,
            'user_age' => $this->user_age,

        ];
        try{

            $result = Db::table('user')->insert($data);
            if(!$result){
                $this->registerError('插入数据库失败');
            }

        } catch (Exception $e) {
            $this->registerError('插入数据库失败');
        }


    }

    private function checkRegisterDate(){
        $rules = [
            'user_name'  => 'require|max:25',
            'user_phone'   => 'require',
            'user_password'   => 'require|max:25',
            'user_nickname'   => 'require|max:20',
            'user_sex'   => 'require',
            'user_age'   => 'number|between:1,120',
        ];

        $message  =   [
            'user_name.require' => '请输入用户名',
            'user_name.max'     => '用户名最多不能超过25个字符',
            'user_phone.require'   => '请输入手机号',
            'user_phone.mobile'   => '手机号格式有误',
            'user_password.require' => '请输入密码',
            'user_password.max'     => '密码最多不能超过25个字符',
            'user_nickname.require' => '请输入昵称',
            'user_nickname.max'     => '昵称最多不能超过25个字符',
            'user_sex'=>'请选择您的性别',
            'user_age.number'   => '年龄必须是数字',
            'user_age.between'  => '年龄只能在1-120之间',

        ];

        $data = [
            'user_name'  => $this->user_name,
            'user_phone'   => $this->user_phone,
            'user_password'   => $this->user_password,
            'user_nickname'   => $this->user_nickname,
            'user_sex'   => $this->user_sex,
            'user_age'   => $this->user_age,
        ];

        $validate = new Validate($rules,$message);
        $result   = $validate->check($data);
        if($result){
            return true;
        }else{
            $this->registerError($validate->getError());
            return false;
        }

    }

    private function savaAvatar(){

        if($this->user_avatar){

            $destinationPath = 'uploads'. DS .'avatar';

            $saveInfo = $this->user_avatar->validate(['ext'=>'jpeg,jpg,png,gif'])->move($destinationPath);

            if($saveInfo){
                $this->user_avatar_path = $saveInfo->getPathname();
                return true;
            }else{
                $this->registerError($this->user_avatar->getError());
                return false;
            }
        }else{
            $this->registerError('请上传头像');
            return false;
        }
    }

    private function registerError($msg){
        $this->register_success = false;
        $this->register_error=$msg;
    }



}
